package com.ecp.commons.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.ecp.commons.annotation.RequireAdminPrivilege;
import com.ecp.commons.annotation.SequenceType;

public class PrivilegeInterceptor implements HandlerInterceptor {
	
	private static final Log	log	= LogFactory.getLog(PrivilegeInterceptor.class);
	
	private boolean checkAfterUrl(String url){
//		try {
//			for(Action action : this.actionService.getAll()){
//				String actionUrl = action.getActionData();
//				if(url.equals(actionUrl)){
//					return true;
//				}
//			}
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return true;
//		}
		return false;
	}
	
//	@Autowired
//	private ActionService actionService;
//	@Autowired
//	private ColumnService columnService;

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HandlerMethod handlerMethod = (HandlerMethod) handler;
//		RequireAdminPrivilege adminPrivilege = handlerMethod.getMethodAnnotation(RequireAdminPrivilege.class);
		String url = request.getRequestURI();
//		System.out.println(request.getRemoteAddr()+":"+url );
		return true;
//		else{
//			if (adminPrivilege == null) {
//				return true;
//			}
//			if (adminPrivilege.sequence().equals(SequenceType.AFTER)) {
//				return true;
//			}
//		}
		
//		log.info("check privilege.");
		
//		Admin admin = (Admin) request.getSession().getAttribute(Global.KEY_BACKEND_USER);
//		if (admin != null) {
//			String sql = "select * from t_action where actionData=?";
//			
//			Action action = this.actionService.getEntity(sql, new Object[]{url});
//			if(action!=null){
//				Collection<Integer> actionIds = (Collection<Integer>)request.getSession().getAttribute(Global.KEY_BACKEND_PRIVILEGE_ACTION);
//				//如果基础的action里包含此action就放过
//				if(actionIds.contains(action.getId())){
//					return true;
//				}
//				
//				Collection<Integer> columnIds = (Collection<Integer>)request.getSession().getAttribute(Global.KEY_BACKEND_PRIVILEGE_COLUMN);
//				//查出所有具备此action的栏目
//				sql = "select c.* from t_column c,t_columnType ct,t_module m,t_action2module a2m,t_action a " +
//						"where c.columnTypeId=ct.id and ct.moduleid=m.id and m.id=a2m.moduleid and a.id=a2m.actionid and a.id=?";
//				List<Column> haveActionColumns = this.columnService.getEntityList(sql, new Object[]{action.getId()});
//				boolean isCan = false;
//				for(Column c : haveActionColumns){
//					if(columnIds.contains(c.getId())){
//						isCan = true;
//						break;
//					}
//				}
//				if(isCan){
//					actionIds.add(action.getId());
//					request.getSession().setAttribute(Global.KEY_BACKEND_PRIVILEGE_ACTION, actionIds);
//					return true;
//				}
//				JSONObject json = new JSONObject();
//				json.put("success", false);
//				json.put("msg", "您无权访问此功能");
//				json.put("reason", "您无权访问此功能");
//				json.put("isPrivilegeError", true);
//				request.setAttribute("json", json);
//				request.getRequestDispatcher("/json.jsp").forward(request, response);
//				return false;
//			}else{
//				return true;
//			}
//			
//		}else{
//			response.sendRedirect(request.getContextPath() + "/adminLogin.do"); 
//			return false;
//		}
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		HandlerMethod handlerMethod = (HandlerMethod) handler;
		RequireAdminPrivilege adminPrivilege = handlerMethod.getMethodAnnotation(RequireAdminPrivilege.class);
		if (adminPrivilege == null) {
			return;
		}
		if (adminPrivilege.sequence().equals(SequenceType.BEFORE)) {
			return;
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
